<?php
/**
 * Page Template
 *
 * @package templateSystem
 * @copyright Copyright 2003-2005 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: tpl_product_reviews_info_default.php 2993 2006-02-08 07:14:52Z birdbrain $
 */
?>
<div class="centerColumn" id="reviewsInfoDefault">

<?php
  if (zen_not_null($products_image)) {
   	/**
     * require the image display code
     */
?>
<div id="reviewsInfoDefaultProductImage" class="centeredContent back"><?php require($template->get_template_dir('/tpl_modules_main_product_image.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_main_product_image.php'); ?></div>
<?php
        }
?>

<div class="centeredContent">
<p> </p>
<h1 id="reviewsInfoDefaultHeading"><?php echo '<a href="' . zen_href_link(zen_get_info_page($_GET['products_id']), zen_get_all_get_params(array('reviews_id'))) . '">'  . $products_name .  '</a>'; ?></h1>
<p> </p>
<h2 class=""><?php echo REVIEW_BY . sprintf(TEXT_REVIEW_BY, zen_output_string_protected($review_info->fields['customers_name'])); ?></h2>
<p> </p>
<?php echo zen_image(DIR_WS_TEMPLATE_IMAGES . 'stars_' . $review_info->fields['reviews_rating'] . '.gif', sprintf(TEXT_OF_5_STARS, $review_info->fields['reviews_rating'])), sprintf(TEXT_OF_5_STARS, $review_info->fields['reviews_rating']) . '<br /><h3>' . $review_info->fields['reviews_title'] ; ?></h3>
<p> </p>
</div>
<div id="productReviews" class="content biggerText"><?php echo zen_break_string(nl2br(zen_output_string_protected(stripslashes($review_info->fields['reviews_text']))), 60, '-<br />'); ?></div>
<div id="reviewsInfoDefaultDate" class="bold"><?php echo sprintf(TEXT_REVIEW_DATE_ADDED, zen_date_short($review_info->fields['date_added'])); ?>&nbsp;</div>


<p>
<?php 
    echo REVIEW_USEFUL3;

    $reviews_votes_sql = "SELECT votes_id FROM votes WHERE reviews_id = :reviewsID AND customers_id = :customerID";
    $reviews_votes_sql = $db->bindVars($reviews_votes_sql, ':reviewsID', $_GET['reviews_id'], 'integer');
    $reviews_votes_sql = $db->bindVars($reviews_votes_sql, ':customerID', $_SESSION['customer_id'], 'integer');
    $reviews_votes_result = $db->Execute($reviews_votes_sql);
    if($reviews_votes_result->EOF)
    {
        echo ' <input type="button" onclick="reviews(1,' . $_GET["reviews_id"] . ')" value="' 
                . REVIEW_YES_BUTTON . '" style="width: 30px" /> <input type="button" onclick="reviews(0,' . $_GET["reviews_id"] . ')" value="' 
                . REVIEW_NO_BUTTON . '" style="width: 30px" /> '; 
    }
    else
    {
        echo " " . ALREADY_VOTES_THIS_REVIEW;
    }
?>
</p>
<div class="centeredContent"><?php 
    $name = "Review " . $products_name . " " . sprintf(TEXT_REVIEW_BY, zen_output_string_protected($review_info->fields['customers_name']));
	$caption = zen_trunc_string(zen_break_string(nl2br(zen_output_string_protected(stripslashes($review_info->fields['reviews_text']))), 40, '<br />'), 100);
    $link_product_info = zen_href_link(FILENAME_PRODUCT_REVIEWS_INFO, zen_get_all_get_params());
    $picture_product = HTTP_SERVER . "/" . $products_image_medium;
    echo '<a href="https://www.facebook.com/dialog/feed?app_id=435254129838889&name=' . $name .'&caption=' . $caption . '&link=' . $link_product_info . '&picture=' . $picture_product . '&redirect_uri=' . $link_product_info . '">' . zen_image_button(BUTTON_IMAGE_SHARE_REVIEW, BUTTON_SHARE_REVIEW_ALT) . '</a>'; 
?>
<?php echo ($reviews_counter > 1 ? '<a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS, zen_get_all_get_params(array('reviews_id'))) . '">' . zen_image_button(BUTTON_IMAGE_MORE_REVIEWS , BUTTON_MORE_REVIEWS_ALT) . '</a>' : ''); ?>
<?php echo '<a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS_WRITE, zen_get_all_get_params(array('reviews_id'))) . '">' . zen_image_button(BUTTON_IMAGE_WRITE_REVIEW, BUTTON_WRITE_REVIEW_ALT) . '</a>'; ?></div>


</div>

<?php
    if(sizeof($reviewsArray) > 0)
    {        
        echo "<h2>" . TEXT_MORE_REVIEW . "</h2>";
        
        $list = listReviews($db, '', $reviewsArray);
        echo '<div id="productReviews">' . $list . '</div>';
    }
?>

<form method="POST" action="<?php echo zen_href_link(FILENAME_PRODUCT_REVIEWS_INFO, zen_get_all_get_params(),'NONSSL') ?>" name="reviews_yes_no">
    <input type="hidden" value="" id="hd_review_yes_no" name="reviews_yes_no" />        
    <input type="hidden" value="" id="hd_review_yes_no_id" name="reviews_yes_no_id" />        
</form>
<?php
//Vu - add this function
function listReviews($db, $parentid, $reviewsArray, $res = '', $level = ''){
    foreach($reviewsArray as $reviews){
        if($reviews['reviewParentId'] == $parentid){
            $re = "<div class='child{$level}'><p>" ;
            if($parentid == NULL){
                $re .= $reviews["reviewsYes"] . " " . REVIEW_USEFUL . " " . $reviews["reviewsVotes"]. " " . REVIEW_USEFUL2 . "<br />" 
                    . zen_image(DIR_WS_TEMPLATE_IMAGES . 'stars_' . $reviews['reviewsRating'] . '.gif') ;
            
                $re .= "<span class='biggerText' style='vertical-align: 7px'><strong><a href='" . zen_href_link(FILENAME_PRODUCT_REVIEWS_INFO, 'products_id=' . (int)$_GET['products_id'] . '&reviews_id=' . $reviews['id']) . "'>" . $reviews["reviewsTitle"] . "</a></strong></span>"
                    . "<span style='vertical-align: 7px'> - <em>" . zen_date_short($reviews['dateAdded']) . "</em></span><br />"
                    . sprintf(TEXT_REVIEW_BY, $reviews['customersName'])."</p>";
            }
            $re .=   "<p class='biggerText' >" .  $reviews['reviewsText'] . "</p>";
            if($parentid == NULL){
                $re .= $reviews["owner"]; 
            }
            $re .= "<p>". REVIEW_USEFUL3;
            $reviews_votes_sql = "SELECT votes_id FROM votes WHERE reviews_id = :reviewsID AND customers_id = :customerID";
            $reviews_votes_sql = $db->bindVars($reviews_votes_sql, ':reviewsID', $reviews['id'], 'integer');
            $reviews_votes_sql = $db->bindVars($reviews_votes_sql, ':customerID', $_SESSION['customer_id'], 'integer');
            $reviews_votes_result = $db->Execute($reviews_votes_sql);

            if($reviews_votes_result->EOF){
                  $re .= ' <input type="button" onclick="reviews(1,' . $reviews["id"] . ')" value="' 
                          . REVIEW_YES_BUTTON . '" style="width: 30px" /> <input type="button" onclick="reviews(0,' . $reviews["id"] . ')" value="' 
                          . REVIEW_NO_BUTTON . '" style="width: 30px" /> '; 
            }
            else{

                $reviews_votes_countyes_sql = "SELECT count(votes_id) as countyes FROM votes WHERE reviews_id = :reviewsID AND yes_no_vote = 1";
                $reviews_votes_countyes_sql = $db->bindVars($reviews_votes_countyes_sql, ':reviewsID', $reviews['id'], 'integer');
                $reviews_votes_countyes_result = $db->Execute($reviews_votes_countyes_sql);

                $reviews_votes_countno_sql = "SELECT count(votes_id) as countno FROM votes WHERE reviews_id = :reviewsID AND yes_no_vote = 0";
                $reviews_votes_countno_sql = $db->bindVars($reviews_votes_countno_sql, ':reviewsID', $reviews['id'], 'integer');
                $reviews_votes_countno_result = $db->Execute($reviews_votes_countno_sql);

               $re .= " " . "VOTES: " . ($reviews_votes_countyes_result->fields['countyes'] - $reviews_votes_countno_result->fields['countno']). " " . ALREADY_VOTES_THIS_REVIEW;
            }

            $re .= REVIEW_USEFUL3;
            $re .= "<input type='button' id='{$reviews["id"]}' class='comment-button' value='Comment'/>";
            $re .= "</p></div>";
            $re .= '<div><a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS_INFO,  'products_id=' . (int)$_GET['products_id'] . '&reviews_id=' . $reviews['id']) . '">' . zen_image_button(BUTTON_IMAGE_READ_REVIEWS, BUTTON_READ_REVIEWS_ALT) . '</a></div><br class="clearBoth" />';
            $res .= listReviews($db, $reviews['id'], $reviewsArray, $re, $level.'1');
        }
    }
    return $res;
}
//Vu - end edit
?>
<script type="text/javascript">
    function reviews(yesno, rid)
    {
        $('#hd_review_yes_no').val(yesno);
        $('#hd_review_yes_no_id').val(rid);
        $('[name=reviews_yes_no]').submit();
    }
    
</script>